﻿using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using LitJson;
using UnityEngine;
using UnityEngine.UI;

public class ReplyAddFriendPanel : BasePanel
{

	public Text msgText;

	public Button agreeBtn;

	public Button refuseBtn;

	private int _friendId;

	private ReplyAddFriendRequest _replyAddFriendRequest;
	
	// Use this for initialization
	void Start () {
		//组件初始化
		_replyAddFriendRequest = this.gameObject.GetComponent<ReplyAddFriendRequest>();
		
		
		//按钮事件函数绑定
		agreeBtn.onClick.AddListener(delegate { AgreeButtonClicked(); });
		refuseBtn.onClick.AddListener(delegate { RefuseButtonClicked(); });
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	/// <summary>
	/// 用于外界刷新显示
	/// </summary>
	/// <param name="msg"></param>
	/// <param name="friendId"></param>
	public void SyncShow(string msg,int friendId)
	{
		msgText.text = msg;
		_friendId = friendId;
	}
	
	private void AgreeButtonClicked()
	{
		// 发起请求
		_replyAddFriendRequest.SendRequest(_friendId,1);
	}


	private void RefuseButtonClicked()
	{
		// 发起请求
		_replyAddFriendRequest.SendRequest(_friendId,2);
	}

	public override void OnEnter()
	{
		
		this.transform.localScale=Vector3.zero;
		if (!this.gameObject.activeSelf)
		{
			this.gameObject.SetActive(true);
		}
		//动画处理
		this.transform.DOScale(new Vector3(1, 1, 1), 0.5f);
	}

	public override void OnPause()
	{
		
	}

	public override void OnResume()
	{
		
	}

	public override void OnExit()
	{
		this.transform.DOScale(Vector3.zero, 0.5f).OnComplete(delegate { this.gameObject.SetActive(false); });
		
	}

	public void HandleResponse(JsonData respJsonData)
	{
		//根据情况开启面板
		if (respJsonData["state"].Equals("success"))
		{
			Debug.Log(respJsonData["msg"].ToString());
		}else if (respJsonData["state"].Equals("fail"))
		{
			Debug.LogError(respJsonData["msg"].ToString());
		}
		
		//最后关掉当前面板
		UnityFacade.Instance.UiManager.PopPanel();	
	}
}
